/*
 * Copyright 2011 derek.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aocore.map.path;

import com.aocore.d2data.AreaLevel;
import com.aocore.map.GameMap;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author derek
 */
public class AOPathFinder extends AStar<AOPathFinder.Node>
{
    protected class Node
    {
        public AreaLevel area;
        public int x;
        public int y;

        Node(AreaLevel area, int x, int y)
        {
            this.area = area;
            this.x = x;
            this.y = y;
        }

        @Override
        public String toString()
        {
            return area + " (" + x + ", " + y + ")";
        }
    }

    private final GameMap gameMap;
    private Node goal;

    public List<Node> path(AreaLevel fromArea, int fromX, int fromY, AreaLevel toArea, int toX, int toY)
    {
        goal = new Node(toArea, toX, toY);

        long begin = System.currentTimeMillis();
        List<Node> result = compute(new Node(fromArea, fromX, fromY));
        long end = System.currentTimeMillis();

        System.out.println("Time = " + (end - begin) + " ms");
        System.out.println("Expanded = " + getExpandedCounter());
        System.out.println("Cost = " + getCost());

        return result;
    }

    public AOPathFinder(GameMap gameMap)
    {
        this.gameMap = gameMap;
    }

    @Override
    protected boolean isGoal(Node node)
    {
        if (node.area == goal.area && node.x == goal.x && node.y == goal.y)
        {
            return true;
        }

        return false;
    }

    @Override
    protected Double g(Node from, Node to)
    {
        if (from.area == to.area && from.x == to.x && from.y == to.y)
        {
            return 0.0;
        }
        else
        {
            return (double) gameMap.getMap(to.area).get(to.x, to.y);
        }
    }

    @Override
    protected Double h(Node from, Node to)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected List<Node> generateSuccessors(Node node)
    {
        List<Node> result = new LinkedList<Node>();

        return result;
    }
}
